//
//  ComplaintViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/10.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ComplaintViewController.h"
#import "DISAcomplaintLsitModel.h"
#import "DISAcompllainListTableViewCell.h"
#import "CStousuCOnViewController.h"

@interface ComplaintViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@end

@implementation ComplaintViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    [self requeset];
    // Do any additional setup after loading the view.
}

-(void)requeset{
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    NSInteger ind = [_type integerValue];
    
    ind-=1;
    NSString *stt  = [NSString stringWithFormat:@"%ld",ind];
    if (ind == -1) {
        stt = @"";
    }
    param[@"state"] = stt;
    [weakSelf.mydateSource removeAllObjects];
    [NetWorkTool POST:complaintLsit param:param success:^(id dic) {
        for (NSDictionary *moDic in [dic objectForKey:@"data"]) {
            [DYModelMaker DY_makeModelWithDictionary:moDic modelKeyword:@"" modelName:@""];
            DISAcomplaintLsitModel *model = [[DISAcomplaintLsitModel alloc]init];
            [model setValuesForKeysWithDictionary:moDic];
            [weakSelf.mydateSource addObject:model];
        }
        [weakSelf.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf.mydateSource removeAllObjects];
        [weakSelf.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [weakSelf.mydateSource removeAllObjects];
        [weakSelf.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
    } needUser:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DISAcompllainListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DISAcompllainListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DISAcomplaintLsitModel *mdeol = _mydateSource[indexPath.section];
    [cell refash:mdeol];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DISAcomplaintLsitModel *model = _mydateSource[indexPath.section];
    CStousuCOnViewController *tousuvc = [[CStousuCOnViewController alloc]init];
    tousuvc.idStr = model.Id;
    [self.navigationController pushViewController:tousuvc animated:YES];
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight-49) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.tableFooterView = [UIView new];
        [_myTableView registerNib:[UINib nibWithNibName:@"DISAcompllainListTableViewCell" bundle:nil] forCellReuseIdentifier:@"DISAcompllainListTableViewCell"];
        adjustInset(_myTableView);
        kWeakSelf;
        weakSelf.myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
            [weakSelf.myTableView.mj_header beginRefreshing];
        }];
        self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self requeset];//请求数据
        }];
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
